﻿namespace HuaWei
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;

    public class DefaultParser
    {
        public static string Extract(string path)
        {
            StringBuilder stringBuilder = new StringBuilder();
            IFilter filter = null;
            string result;
            try
            {
                filter = DefaultParser.UrpGlyJoL(path);
                if (filter == null)
                {
                    result = string.Empty;
                    return result;
                }
                uint num = 0u;
                STAT_CHUNK sTAT_CHUNK = default(STAT_CHUNK);
                if (filter.Init(IFILTER_INIT.CANON_PARAGRAPHS | IFILTER_INIT.HARD_LINE_BREAKS | IFILTER_INIT.CANON_HYPHENS | IFILTER_INIT.CANON_SPACES | IFILTER_INIT.APPLY_INDEX_ATTRIBUTES | IFILTER_INIT.APPLY_CRAWL_ATTRIBUTES | IFILTER_INIT.APPLY_OTHER_ATTRIBUTES | IFILTER_INIT.SEARCH_LINKS | IFILTER_INIT.FILTER_OWNED_VALUE_OK, 0u, null, ref num) != 0)
                {
                    throw new Exception("Problem initializing an IFilter for:\n" + path + " \n\n");
                }
                while (filter.GetChunk(out sTAT_CHUNK) == 0)
                {
                    if (sTAT_CHUNK.flags == CHUNKSTATE.CHUNK_TEXT)
                    {
                        GEnum gEnum = GEnum.S_OK;
                        while (gEnum == GEnum.S_OK || gEnum == GEnum.FILTER_S_LAST_TEXT)
                        {
                            uint num2 = 65536u;
                            StringBuilder stringBuilder2 = new StringBuilder(65536);
                            gEnum = (GEnum)filter.GetText(ref num2, stringBuilder2);
                            if (num2 > 0u && stringBuilder2.Length > 0)
                            {
                                if ((long)stringBuilder2.Length < (long)((ulong)num2))
                                {
                                    num2 = (uint)stringBuilder2.Length;
                                }
                                stringBuilder.Append(stringBuilder2.ToString(0, (int)num2));
                                stringBuilder.Append(" ");
                            }
                        }
                    }
                }
            }
            finally
            {
                if (filter != null)
                {
                    Marshal.ReleaseComObject(filter);
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            result = stringBuilder.ToString();
            return result;
        }

        public static bool IsParseable(string filename)
        {
            return (UrpGlyJoL(filename) != null);
        }

        [DllImport("query.dll", CharSet=CharSet.Unicode)]
        private static extern int LoadIFilter(string str, [MarshalAs(UnmanagedType.IUnknown)] object obj, ref IFilter ifilter);
        private static IFilter UrpGlyJoL(string str)
        {
            object obj2 = null;
            IFilter ifilter = null;
            if (LoadIFilter(str, obj2, ref ifilter) != 0)
            {
                return null;
            }
            return ifilter;
        }
    }
}

